(ns admin-framework.util)

(import 'java.util.concurrent.Executors
        'java.net.InetSocketAddress
        'org.jboss.netty.channel.Channels
        'org.jboss.netty.channel.ChannelFactory
        'org.jboss.netty.buffer.ChannelBuffers
        'org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
        'org.jboss.netty.bootstrap.ServerBootstrap
        'org.jboss.netty.channel.SimpleChannelHandler
        'org.jboss.netty.channel.group.DefaultChannelGroup)

(defn create-svr
  "create and return a netty server bootstrap"
  [handler-func]
  (let [fac (NioServerSocketChannelFactory. (Executors/newCachedThreadPool) 
                                            (Executors/newCachedThreadPool))
        boot (ServerBootstrap. fac)]
    (.setOption boot "child.tcpNoDelay" true)
    (.setOption boot "child.keepAlive" true)
    (handler-func boot)
    boot))

(defn start-svr [port handler-func]
  (.bind (create-svr handler-func) (InetSocketAddress. port)))